package com.cc.Controller;

import com.cc.Services.NewsServicesImpl;
import com.cc.entity.News;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


@Controller
@RequestMapping("/news")
public class NewsController {
    @Autowired
    NewsServicesImpl newsServicesImpl;

    @RequestMapping("/list")
    public String list(@RequestParam(value = "page",defaultValue = "1")int page,
                             @RequestParam(value = "pageSize",defaultValue = "3") int pageSize,Model model){
        //调用service
        List<News> news=newsServicesImpl.listByPage(page,pageSize);
        double no=(double) newsServicesImpl.total()/pageSize;
        int count=(int)Math.ceil(no);
        model.addAttribute("news",news);
        model.addAttribute("curPage",page);
        model.addAttribute("count",count);
        return "manage/news";
    }
  @RequestMapping("/delete")
    public String delete(Integer enId){
        int result=newsServicesImpl.deleteByid(enId);
        if(result==0){
            return "manage/error";
        }
        return "manage/manage-result";
    }
    @RequestMapping("/edit")
    public String edit(Model model,int enId){
        News news=newsServicesImpl.selectByCondition(enId);
        model.addAttribute("news",news);
        return "manage/news-modify";
    }

    @RequestMapping("/editSubmit")
    public String editSubmit(News news){
        int result=newsServicesImpl.updateById(news);
        if(result<=0){
            return "manage/error";
        }
        return "manage/manage-result";
    }
    @RequestMapping("/addnews")
    public String addnews(Model model) {
        return "manage/news-add";
    }
    @RequestMapping("/add")
    public String add(Model model,News news) throws Exception{
        //@DateTimeFormat
   /*  SimpleDateFormat d=new SimpleDateFormat("YYYY-MM-DD HH:mm:ss");
        String newdate=d.format(new Date());

        DateTimeStringConverter
        System.out.println(newdate);
       // news.*/

        //model.addAttribute("news",news);
        return "manage/news";
    }
}
